package cn.net.wanji.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Random;

public class ImageUtils {

    public static void main(String[] args) {

//        FileReader fileReader = new FileReader("原始数据.json");
//        List<String> readLines = fileReader.readLines();
//
//        JSONObject object = JSON.parseObject(readLines.get(0));
//
//        JSONObject vehiclePicInfos = object.getJSONArray("vehiclePicInfos").getJSONObject(0);
//
//        String headImage = vehiclePicInfos.getString("headImage");
//        String sideImage = vehiclePicInfos.getString("sideImage");
//        String licenseImage = vehiclePicInfos.getString("licenseImage");
//
//        generateImage(headImage, "./headImage.jpg");
//        generateImage(sideImage, "./sideImage.jpg");
//        generateImage(licenseImage, "./licenseImage.jpg");

    }


    /**
     * @param file base64编码字符串
     * @param path 图片路径-具体到文件
     * @return
     * @Description: 将base64编码字符串转换为图片
     * @Author:
     * @CreateTime:
     */
    public static void generateImage(String file, String path) {
        try {
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] b = decoder.decode(file);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            // 保存图片
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //length用户要求产生字符串的长度
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }


    public static List<File> multipartFileToFile(MultipartFile[] multipartFiles) {
        List<File> list = new ArrayList<>();
        for (MultipartFile multipartFile : multipartFiles) {
            // 文件上传前的名称
            String fileName = multipartFile.getOriginalFilename();
            File file = new File(fileName);
            OutputStream out = null;
            try {
                // 获取文件流，以文件流的方式输出到新文件
                //    InputStream in = multipartFile.getInputStream();
                out = new FileOutputStream(file);
                byte[] ss = multipartFile.getBytes();
                for (int i = 0; i < ss.length; i++) {
                    out.write(ss[i]);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            list.add(file);
        }

        return list;
    }

}
